﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.Web
{
    public partial class SearchFriend : BasePageVisitor
    {
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.BLL.NJL_MYFRIEND bllFriend = new BLL.NJL_MYFRIEND();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtKeywords.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; ");
                txtKeywords.Text = Request.QueryString["user"];
                bind();
            }
        }
        void bind()
        {
            Page.Title = Request.QueryString["keywords"] + "-农家乐网搜索结果";
            DataSet ds = bllPerson.GetListUserInPerson("a.USER_NAME like '%" + Request.QueryString["user"] + "%'");
            if (ds.Tables[0].Rows.Count > 15)
            {
                pager.InnerHtml = Royalsoft.Common.Pager.GetPageNum(ds, rptUserList, 15, null);
            }
            else
            {
                rptUserList.DataSource = ds;
                rptUserList.DataBind();
            }

            litSearchCount.Text = ds.Tables[0].Rows.Count.ToString();
            #region 最新会员
            ds = bllPerson.GetList(9, "1=1", "a.CREATE_DATE_TIME desc");
            rptNewUser.DataSource = ds;
            rptNewUser.DataBind();
            #endregion
            #region 人气用户
            ds = bllPerson.GetList(3, "1=1", "a.VISIT_COUNT desc");
            rptHotUser.DataSource = ds;
            rptHotUser.DataBind();
            #endregion
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("SearchFriend.aspx?user=" + txtKeywords.Text.Trim());
        }
        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litAttention") as Literal;
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string id = drv["ID"].ToString();
                if (UserId != "" && UserName != "")
                {
                    DataSet dsFriend = bllFriend.GetList("NJL_MYFRIEND.FRIEND_USER_ID=" + id + " and NJL_MYFRIEND.NJL_USER_ID=" + UserId);
                    if (UserId.Equals(id) && UserType == "2")
                    {
                        lit.Text = "";
                    }
                    else if (!UserId.Equals(id) && UserType == "2")
                    {
                        if (dsFriend.Tables[0].Rows.Count > 0)
                        {
                            lit.Text = "<a href=javascript:DeleteUser('" + id + "')>-&nbsp;取消关注</a>";
                        }
                        else
                        {
                            lit.Text = "<a href=javascript:AddUser('" + id + "')>+&nbsp;添加关注</a>";
                        }
                    }
                }
                else
                {
                    lit.Text = "<a href=javascript:AddUser('" + id + "')>+&nbsp;添加关注</a>";
                }
            }
        }
        protected void rptHotUser_ItemDataBound1(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                Literal lit = e.Item.FindControl("litAddgz") as Literal;
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string id = drv["ID1"].ToString();
                if (UserId != "" && UserName!="")
                {
                    DataSet dsFriend = bllFriend.GetList("NJL_MYFRIEND.FRIEND_USER_ID=" + id + " and NJL_MYFRIEND.NJL_USER_ID=" + UserId);
                    if (UserId.Equals(id)&&UserType=="2")
                    {
                        lit.Text = "";
                    }
                    else if (!UserId.Equals(id) && UserType == "2")
                    {
                        if (dsFriend.Tables[0].Rows.Count > 0)
                        {
                            lit.Text = "<a href=javascript:DeleteUser('" + id + "')>-&nbsp;取消关注</a>";
                        }
                        else
                        {
                            lit.Text = "<a href=javascript:AddUser('" + id + "')>+&nbsp;添加关注</a>";
                        }

                    }
                }
                else
                {
                    lit.Text = "<a href=javascript:AddUser('" + id + "')>+&nbsp;添加关注</a>";
                }
            }
        }
    }

}